	import model.CompanyProfileModel;
	import mx.controls.Alert;
	import mx.rpc.xml.SimpleXMLEncoder;
	import nrb.utils.DateHandler;
	import nrb.utils.XMLHelper;
	
	
	
	[Bindable] private var xml_company_profile:XML;
	private var helper_xml:XMLHelper;
	public var company_profile_model:CompanyProfileModel;
	private var dates:DateHandler;
	private var pA:Object;
	
	public function func_init(): void {
		xml_company_profile = new XML();
		helper_xml = new XMLHelper();
		company_profile_model = new CompanyProfileModel();
		dates = new DateHandler();
		pA = new Object();
		
		pA = parentApplication;
		get_company_profile();
	}
	
	private function get_company_profile(): void {
		//check if xml already exists
		if (!helper_xml.func_check_fileexists(pA.FILENAME_COMPANY_PROFILE_XML)){
			func_new();
		}else{
			helper_xml.func_open_xml(pA.FILENAME_COMPANY_PROFILE_XML);
			xml_company_profile = helper_xml.func_get_xml();
			func_init_fields();
		}
	}
	
	private function func_init_fields(): void {
		if(xml_company_profile != null){
			company_profile_model.func_set_attributes(xml_company_profile);
			txt_name.text = company_profile_model.name;
			txt_address.text = company_profile_model.address;
			txt_phone_number.text = company_profile_model.phone_number;
			txt_tin.text = company_profile_model.tin;
			txt_header.text = company_profile_model.header;
			txt_footer.text = company_profile_model.footer;
		}else{
			
		}
	}
	
	private function func_update(): void {
		xml_company_profile.name = txt_name.text;
		xml_company_profile.address = txt_address.text;
		xml_company_profile.phone_number = txt_phone_number.text;
		xml_company_profile.tin = txt_tin.text;
		xml_company_profile.header = txt_header.text;
		xml_company_profile.footer = txt_footer.text;
		//write to xml file
		helper_xml.func_write_xml(xml_company_profile);
		//update global variable holding company profile object
		pA.COMPANY_PROFILE.func_set_attributes(xml_company_profile);
		Alert.show('Company Profile Updated');
	}
	
	//creates user if xml file not found
	private function func_new(): void {
		var parent_xml:XML = new XML("");
		helper_xml.func_create_xml(parent_xml, pA.FILENAME_COMPANY_PROFILE_XML);
		
		//set user attributes
		company_profile_model = new CompanyProfileModel();
		company_profile_model.name = '';
		company_profile_model.address = '';
		company_profile_model.phone_number = '';
		company_profile_model.tin = '';
		company_profile_model.header = '';
		company_profile_model.footer = '';
		company_profile_model.date_modified = dates.get_full_date('YYYY-MM-DD HH:NN:SS');
		company_profile_model.modified_by = pA.LOGGED_USER_ID;
		
		helper_xml.func_open_xml(pA.FILENAME_COMPANY_PROFILE_XML);
		xml_company_profile = helper_xml.func_get_xml();
		
		//object to user xml node
		var q_name:QName = new QName('company_profile');
		var xml_doc:XMLDocument = new XMLDocument();
		var xml_encoder:SimpleXMLEncoder = new SimpleXMLEncoder(xml_doc);
		var xml_node:XMLNode = xml_encoder.encodeValue(company_profile_model,q_name,xml_doc);
		var company_xml_node:XML = new XML(xml_doc.toString());		
		xml_company_profile = new XML(company_xml_node);
		helper_xml.func_write_xml(xml_company_profile);
	}
	
	